home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / PART_F / VIEWMAN / DAVEINIT.S < prev    next >
Encoding:
Text File  |  2001-02-10  |  45.4 KB  |  2,409 lines

  1. *
  2. *   DAVEINIT.S
  3. *
  4.     .include    "equates.s"
  5. *
  6.     .text
  7. *
  8. *
  9. *
  10. DaveInit:
  11.     movem.l    a0-a6/d1-d7,-(sp)
  12. *
  13.     move.l    a0,BshEnvir    ;save the brush environment string
  14. *
  15. *  Check out our system
  16. *
  17.     pea    getcook(pc)    ;check cookie jar for shifter type
  18.     move.w    #$26,-(sp)
  19.     trap    #14
  20.     addq.l    #6,sp
  21. *
  22.     moveq    #Busy,d0
  23.     bsr    Graf_Mouse
  24. *
  25.     lea    version,a0
  26.     move.w    #WORLD,d0
  27.     bsr    DaveName
  28. *
  29. *
  30. *  Set-up initial irregular tile variables
  31. *
  32.     move.w    #-56,d0        ;tilex
  33.     move.w    #0,d1        ;tiley
  34.     move.w    #-7,d2        ;tilexrow
  35.     move.w    #-18,d3        ;tileyrow
  36.     move.w    #7,d4        ;tilexcol
  37.     move.w    #0,d5        ;tileycol
  38.     movem.w    d0-d5,Itile
  39. *
  40.     lea    wstruct,a5
  41.     move.w    #256,w_wid(a5)
  42.     move.w    #384,w_hite(a5)
  43. *
  44.     clr.l    xoffset(a5)
  45.     clr.l    lxoffset(a5)
  46. *
  47.     clr.w    blistexist    ;assume BRUSH.LST does not exist    
  48. *
  49. *
  50. *  Now, let's look at LineA variables
  51. *
  52.     dc.w    $a000        ;get lineA base in a0
  53.     lea    -602(a0),a1
  54.     move.l    a1,mouseptr
  55. *
  56.     lea    -348(a0),a1
  57.     move.l    a1,butnptr
  58. *
  59.     move.w    #2,-(sp)    ;get physical screen base
  60.     trap    #14
  61.     addq.l    #2,sp
  62.     move.l    d0,oldphyz
  63. *
  64.     move.w    #4,-(sp)    ;get rez
  65.     trap    #14
  66.     addq.l    #2,sp
  67.     move.w    d0,oldrez    ;save
  68. *
  69.     move.w    form_width,d0    ;get xrez of screen
  70.     move.w    d0,xmax_clp    ;use for clipping
  71.     addq.w    #1,d0
  72.     lsr.w    #1,d0
  73.     move.w    d0,scr_nxln
  74.     move.w    #8,scr_nxwd
  75.     move.w    #2,scr_nxpl
  76. *
  77.     move.w    form_height,ymax_clp
  78.     clr.w    xmin_clp
  79.     clr.w    ymin_clp
  80.     lea    oldpal,a4
  81.     moveq    #0,d4
  82. getpal:
  83.     move.w    #-1,-(sp)
  84.     move.w    d4,-(sp)
  85.     move.w    #7,-(sp)    ;get original palette
  86.     trap    #14
  87.     addq.l    #6,sp
  88.     move.w    d0,(a4)+
  89.     addq.w    #1,d4
  90.     cmpi.w    #16,d4
  91.     bcs    getpal
  92. *
  93. *
  94. *    Get some memory from this puppy
  95. *
  96.     tst.l    bshgraf        ;have we ever Malloc'd before?
  97.     bne    leavmem        ;br if we got our memory already
  98. *
  99.     move.l    #-1,-(sp)
  100.     move.w    #$48,-(sp)    ;how much memory is there?
  101.     trap    #1
  102.     addq.l    #6,sp
  103. *
  104.     lsr.l    #1,d0
  105.     move.l    d0,d1        ;just ask for 3/4 of it
  106.     lsr.l    #1,d1
  107.     add.l    d1,d0
  108. *
  109. *    lsr.l    #1,d1
  110. *    add.l    d1,d0        ;7/8
  111. *
  112.     bclr.l    #0,d0        ;d0 = 3/4 of largest block available
  113. *
  114. *  Ask for 3/4 of all available
  115. *
  116.     move.l    d0,-(sp)
  117.     move.w    #$48,-(sp)
  118.     trap    #1
  119.     addq.l    #2,sp
  120.     move.l    (sp)+,d1
  121. *
  122.     move.l    d0,bshgraf
  123.     add.l    d1,d0
  124.     move.l    d0,malloend    ;save end of our memory (+1)            
  125. *
  126. leavmem:
  127. *
  128. *
  129.     move.w    #-1,worldtile    ;indicate no tile for world
  130. *
  131.     move.l    #undbuf+4096,undform    ;we'll use this buffer for error reporting
  132. *
  133. *   Build the path for BRUSH.LST file
  134. *
  135.     move.l    BshEnvir,a0
  136.     lea    BshListPath,a1    ;build a path for BRUSH.LST here
  137.     move.l    a1,a2
  138. *
  139. Belp:
  140.     move.b    (a0)+,(a1)+
  141.     bne    Belp        ;get a copy of user environment
  142. *
  143.     subq.l    #1,a1
  144. Benvchk:
  145.     move.b    -(a1),d0
  146.     cmp.l    a1,a2
  147.     bcc    Benuff        ;br if just append "BRUSH.LST"
  148. *
  149.     cmpi.b    #"\",d0
  150.     beq    Benuff1        
  151.     cmpi.b    #":",d0
  152.     bne    Benvchk
  153. Benuff1:
  154.     addq.l    #1,a1
  155. Benuff:
  156.     lea    bshpath,a0    ;append "BRUSH.LST"
  157. Belp1:
  158.     move.b    (a0)+,(a1)+
  159.     bne    Belp1
  160. *
  161. *   path for BRUSH.LST is built
  162. *
  163.     clr.w    -(sp)        ;open "BRUSH.LST" for read only
  164.     move.l    a2,-(sp)
  165.     move.w    #$3d,-(sp)
  166.     trap    #1
  167.     addq.l    #8,sp
  168. *
  169.     clr.b    brushlst        ;init bshlist by
  170. *                ;putting terminator in (a null list)
  171. *
  172.     move.w    d0,handle
  173.     bpl    blstxist    ;br if "BRUSH.LST" exists (let's read it in)
  174. *
  175. *  else, read all brush filenames in current directory to build brush list
  176. *
  177.     pea    dma(pc)
  178.     move.w    #$1a,-(sp)    ;set dma for search 1st/next
  179.     trap    #1
  180.     addq.l    #6,sp
  181. *
  182.     clr.w    intrinscnt    ;start with brush #0
  183. *
  184.     lea    brushlst,a1
  185.     lea    blistrng,a0
  186. copheadr:
  187.     move.b    (a0)+,(a1)+
  188.     bne    copheadr    ;put in a header to make it look pretty
  189.     subq.l    #1,a1
  190. *
  191.     move.l    a1,-(sp)    ;** .9e save brush list ptr
  192.     
  193.     clr.w    -(sp)        ;normal search attributes
  194.     move.l    BshEnvir,-(sp)    ;use user supplied search string
  195.     move.w    #$4e,-(sp)    ;search 1st  "*.BSH"
  196.     trap    #1
  197.     addq.l    #8,sp
  198. *
  199.     move.l    (sp)+,a1    ;** .9e
  200. *
  201.     tst.w    d0
  202.     bmi    bshend        ;br if no brush files present
  203.     bra    gotbsh
  204. getbshlp:
  205. *
  206. *  Now get next brush
  207. *
  208.     move.l    a1,-(sp)    ;save brushlst ptr
  209. *
  210.     move.w    #$4f,-(sp)    ;search next
  211.     trap    #1
  212.     addq.l    #2,sp
  213. *
  214.     move.l    (sp)+,a1    ;reget brushlst ptr
  215. *
  216.     tst.w    d0
  217.     bmi    bshend
  218. gotbsh:
  219.     move.w    intrinscnt,d0    ;get intrinsic brush count
  220.     bsr    decit        ;put object #
  221.     move.b    #tab,(a1)+
  222.     lea    nexfile,a0
  223.     moveq    #-1,d0
  224. cbshname:
  225.     addq.w    #1,d0
  226.     move.b    (a0)+,(a1)+
  227.     bne    cbshname
  228.     subq.l    #1,a1
  229.     cmpi.b    #8,d0        ;is filename less than 8 chars?
  230.     bcc    more8
  231.     move.b    #tab,(a1)+
  232. more8:
  233.     move.b    #tab,(a1)+
  234. *
  235. *  now generate a fake symbol name
  236. *
  237.     move.b    #"O",(a1)+
  238.     move.b    #"B",(a1)+
  239.     move.b    #"J",(a1)+
  240.     move.b    #"_",(a1)+
  241.     move.w    intrinscnt,d0
  242.     bsr    decit
  243.     move.b    #cr,(a1)+
  244.     move.b    #lf,(a1)+
  245.     addq.w    #1,intrinscnt
  246.     bra    getbshlp
  247. *
  248. *  Read in existing brushlist file
  249. *
  250. blstxist:
  251.     pea    brushlst    ;read brush list file
  252.     move.l    #bshbufz,-(sp)    ;max buffer size
  253.     move.w    handle,-(sp)
  254.     move.w    #$3f,-(sp)
  255.     trap    #1
  256.     addq.l    #8,sp
  257. *
  258. *
  259.     move.l    (sp)+,a1
  260.     tst.l    d0
  261.     bpl    setexist
  262.     move.w    #-1,d0
  263.     bsr    senderr        ;tell 'em a read error on BRUSH.LST    
  264.     bra    bshend
  265. *
  266. senderr:
  267.     move.l    a0,-(sp)
  268.     move.l    undform,a0
  269.     move.w    d0,(a0)+
  270.     move.l    a0,undform
  271.     move.l    (sp)+,a0
  272.     rts
  273. *
  274. *
  275. *
  276. setexist:
  277.     move.w    #-1,blistexist    ;indicate that the BRUSH.LST file exists
  278.     add.l    d0,a1
  279. bshend:
  280.     clr.b    (a1)
  281. *
  282. *  Now, read in the brushes, convert .IFF to ST/TT and build an entry
  283. *
  284.     clr.w    maxintrins    ;clear max intrinsic brush #
  285.     clr.w    intrinscnt    ;clear # of intrinsic brushes loaded
  286.     clr.w    maxbrshwid    ;clear maximum brush width
  287. *
  288.     move.l    bshgraf,a0        ;here's where we put expanded grafix
  289.     lea    brushlst,a1
  290. rdbrush:
  291. getnumer:
  292.     move.b    (a1)+,d0        ;search for next numeric character
  293.     beq    endblist        ;br if reached list terminator
  294. *
  295.     cmpi.b    #cr,d0        ;see if we got a carriage return
  296.     bne    nocrhere    ;br if not a cr
  297. *
  298. *  we got a CR-- look for 1st printing char
  299. *
  300. stillnon:
  301.     move.b    (a1)+,d0
  302.     beq    endblist    ;br if reached list terminator
  303.     cmpi.b    #" ",d0        ;check for printing char after CR
  304.     bcs    stillnon    ;br if still a non-printing char
  305.     cmpi.b    #"*",d0        ;comment line?
  306.     beq    parsec        ;br if so--parse comment line
  307.     cmpi.b    #";",d0        ;comment line?
  308.     bne    nocrhere    
  309. parsec:
  310.     move.b    (a1)+,d0
  311.     cmpi.b    #cr,d0
  312.     bne    parsec        ;wait til comment line is terminated
  313.     bra    stillnon
  314. *
  315. nocrhere:
  316.     subi.b    #"0",d0
  317.     bcs    getnumer
  318.     cmpi.b    #10,d0
  319.     bcc    getnumer
  320.     subq.l    #1,a1
  321. *
  322.     moveq    #0,d0
  323. nxdec:
  324.     moveq    #0,d1
  325.     move.b    (a1)+,d1
  326.     cmpi.b    #" ",d1            ;reached obj# terminator?
  327.     beq    gotcount
  328.     cmpi.b    #tab,d1            ;tab or blank terminates
  329.     beq    gotcount
  330.     subi.b    #"0",d1
  331.     mulu    #10,d0
  332.     add.w    d1,d0
  333.     bra    nxdec    
  334. gotcount:
  335.     move.b    (a1)+,d1
  336.     cmpi.b    #" ",d1
  337.     beq    gotcount
  338.     cmpi.b    #tab,d1
  339.     beq    gotcount    ;jump over white space
  340.     subq.l    #1,a1
  341. *
  342.     lea    bshblock,a2
  343.     move.w    d0,brushnbr    ;save this brush number
  344. *
  345.     moveq    #0,d0
  346.     move.w    maxintrins,d0    ;** v0.9e
  347. *
  348.     addq.w    #1,maxintrins    ;** v0.9e
  349. *
  350. *    cmp.w    maxintrins,d0    ;** v0.9e
  351. *    bcs    notnew        ;** v0.9e
  352. *    move.w    d0,maxintrins    ;** v0.9e
  353. notnew:
  354.     lsl.l    #4,d0    ;*16
  355.     add.l    d0,a2    ;a2 -> entry in Brush Block (bshblock)
  356.  
  357.     lea    palblock,a3
  358.     lsl.l    #1,d0    ;*32
  359.     add.l    d0,a3    ;a3 -> entry in Palette Block (palblock)    
  360. *
  361. *
  362.     lea    brshfile,a4
  363.     move.l    BshEnvir,a5    ;build path from user defined environment
  364. copenvir:
  365.     move.b    (a5)+,(a4)+
  366.     bne    copenvir
  367.     subq.l    #1,a4
  368. *
  369.     lea    brshfile,a5
  370. copenvlp:
  371.     move.b    -(a4),d0
  372.     cmp.l    a4,a5
  373.     bcc    copenv0
  374.     cmpi.b    #"\",d0
  375.     beq    copenv1
  376.     cmpi.b    #":",d0
  377.     bne    copenvlp
  378. copenv1:
  379.     addq.l    #1,a4
  380. copenv0:
  381. *
  382. copname:
  383.     move.b    (a1)+,d0
  384.     move.b    d0,(a4)+
  385.     cmpi.b    #tab,d0
  386.     beq    namecopd
  387.     cmpi.b    #" ",d0
  388.     beq    namecopd
  389.     cmpi.b    #13,d0
  390.     bne    copname
  391. namecopd:
  392.     clr.b    -(a4)        ;put in terminator
  393. *
  394. *  Now advance a1 to start of symbol name field
  395. *
  396. tosymf:
  397.     move.b    (a1)+,d0
  398.     cmpi.b    #tab,d0
  399.     beq    tosymf
  400.     cmpi.b    #" ",d0
  401.     beq    tosymf
  402.     subq.l    #1,a1        ;a1 -> symbol name string
  403. *
  404.     movem.l    a0-a3,-(sp)
  405. *
  406.     clr.w    -(sp)        ;open next .IFF brush file for read only
  407.     pea    brshfile
  408.     move.w    #$3d,-(sp)
  409.     trap    #1
  410.     addq.l    #8,sp
  411. *
  412.     movem.l    (sp)+,a0-a3
  413. *
  414.     move.w    d0,handle
  415.     bmi    bad_nf        ;br if file not found
  416. *
  417.     movem.l    a0-a3,-(sp)
  418. *
  419.     pea    grafbsh        ;use grafbsh buffer temporarily
  420.     move.l    #maxBSHsize,-(sp)    ;max size we allow
  421.     move.w    handle,-(sp)
  422.     move.w    #$3f,-(sp)    ;read in this brush file
  423.     trap    #1
  424.     adda.w    #12,sp
  425. *
  426.     move.l    d0,-(sp)    ;**.9e save # of bytes read
  427.     move.w    handle,-(sp)    ;**.9e 
  428.     move.w    #$3e,-(sp)    ;**.9e close file
  429.     trap    #1        ;**.9e 
  430.     addq.l    #4,sp        ;**.9e 
  431.     move.l    (sp)+,d0    ;**.9e 
  432.  
  433.     movem.l    (sp)+,a0-a3
  434.     tst.l    d0        ;did we get a good one
  435.     bmi    bad_read    ;br if bad read
  436. *
  437. *   Now parse the .IFF file
  438. *
  439.     lea    grafbsh,a4
  440.     lea    textiff,a5
  441. *
  442.     move.l    (a5)+,d1    ;pick up "FORM"
  443.     cmp.l    (a4)+,d1
  444.     bne    bad_notiff    ;br if not in 1st 4 char position
  445.  
  446.     subq.l    #8,d0        ;do file length check
  447.     cmp.l    (a4)+,d0    ;we'll let this slide for now
  448. *    bne    bad_notiff    ;br if not in agreement
  449.     
  450.     move.l    (a5)+,d1
  451.     cmp.l    (a4)+,d1    ;check "ILBM"
  452.     bne    bad_notiff
  453.  
  454.     move.l    (a5)+,d1
  455.     cmp.l    (a4)+,d1    ;check "BMHD"
  456.     bne    bad_notiff
  457.  
  458.     move.l    (a4)+,d1    ;we want 20 for a BMHD chunk
  459.     cmpi.l    #20,d1        ;did we get it?
  460.     bne    bad_notiff
  461. *
  462.     move.w    (a4)+,d0    ;get width
  463.  
  464.     cmp.w    maxbrshwid,d0    ;check for maximum brush width
  465.     bls    notmxwid
  466.     move.w    d0,maxbrshwid    ;save new maximum
  467. notmxwid:
  468.     move.w    d0,(a2)+    ;save
  469.     move.w    (a4)+,d1
  470.     move.w    d1,(a2)+    ;save height
  471.     addq.l    #4,a4        ;skip leftx/topy
  472. *
  473.     move.l    a0,(a2)+    ;save s_form base ptr to the graphics
  474.     move.l    a1,(a2)+    ;save id_string (terminated by <cr>)
  475. *
  476.     add.w    #15,d0        ;find form width
  477.     andi.w    #$fff0,d0    ;from block width
  478.     lsr.w    #1,d0        ;d0 = s_nxln
  479. *
  480.     mulu    d0,d1
  481.     move.w    d1,(a2)+    ;save mask_off, offset to mask form
  482.     move.w    d0,(a2)+    ;save s_nxln (offset to next line in plane)
  483. *    
  484.     cmpi.b    #4,(a4)+    ;we expect 4 bitplanes
  485.     bne    bad_not16    ;br if not 16 color mode
  486. *
  487.     addq.l    #1,a4        ;skip masking
  488.     move.b    (a4)+,d0    ;compression (0=off, 1=on)
  489.     adda.w    #9,a4        ;skip padding, transparent color,etc.
  490. *
  491.     move.l    (a5)+,d1    ;d1="CMAP"
  492.     move.l    (a5)+,d2    ;d2="BODY"
  493. *
  494. checklp:
  495.     move.l    (a4)+,d3    ;get next literal string
  496.     cmp.l    d3,d1        ;is it a CMAP?
  497.     bne    notcmap        ;br if not
  498. *
  499. *  we're a color map, let's decode
  500. *
  501.     cmp.l    #48,(a4)+    ;is this our kind of color map?
  502.     bne    bad_not16
  503. *
  504.     tst.w    firstpal    ;check if this is our first palette
  505.     bpl    not1st
  506.     move.w    #0,firstpal
  507. not1st:
  508.     moveq    #15,d3        ;get 16 colors
  509. colorlp:
  510.     moveq    #0,d4
  511.     move.b    (a4)+,d4
  512.     lsl.w    #3,d4
  513.     tst.b    d4        ;check lsb in bit7
  514.     bpl    clp1
  515.     bset    #11,d4        ;copy to bit11
  516. clp1:
  517.     move.b    (a4)+,d4
  518.     lsr.b    #1,d4
  519.     btst    #3,d4
  520.     beq    clp2
  521.     bset    #7,d4
  522. clp2:
  523.     andi.w    #$fff0,d4
  524.     move.b    (a4)+,d5
  525.     lsr.b    #5,d5
  526.     bcc    clp3
  527.     bset    #3,d5
  528. clp3:
  529.     or.b    d5,d4
  530.     move.w    d4,(a3)+    ;save next palette entry
  531.     dbra    d3,colorlp
  532.     move.l    (a4)+,d3    ;get next chunk type
  533.  
  534. notcmap:
  535.     cmp.l    d3,d2        ;is this a body?
  536.     beq    gotbody        ;br if so
  537. *
  538. *  we're neither a color map nor a body
  539. *
  540.     move.l    (a4)+,d3
  541.     add.l    d3,a4
  542.     bra    checklp        ;go for next literal string
  543. gotbody:
  544.     move.l    (a4)+,d3    ;get length
  545.     move.l    a4,a5
  546.     add.l    d3,a5        ;a5-> 1 byte beyond end of brush data
  547. *
  548.     tst.b    d0        ;is compression on or off
  549.     beq    ncomprs        ;br if not compressed
  550. *
  551. *  We're compressed..
  552. *
  553. compres:
  554.     move.b    (a4)+,d2    ;get next run length
  555.     ext.w    d2
  556.     bmi    replicat    ;br if not literal
  557. literlp:
  558.     move.b    (a4)+,(a0)+
  559.     dbra    d2,literlp
  560.     cmp.l    a5,a4
  561.     bcs    compres
  562.     bra    grafset
  563. replicat:
  564.     move.b    (a4)+,d1    ;get byte to replicate
  565.     neg.w    d2
  566. replicat1:
  567.     move.b    d1,(a0)+
  568.     dbra    d2,replicat1
  569.     cmp.l    a5,a4
  570.     bcs    compres
  571.     bra    grafset
  572. *
  573. *  we're not compressed
  574. *
  575. ncomprs:            
  576.     move.b    (a4)+,(a0)+
  577.     cmp.l    a5,a4
  578.     bcs    ncomprs
  579. *    
  580. *  we're done getting the "BODY" in place
  581. *
  582. *   Now, make mask form corresponding to this brush form
  583. *
  584. grafset:
  585.     move.l    -12(a2),a5        ;get s_form, this brush
  586.     move.w    -2(a2),d0        ;get s_nxln
  587.     lsr.w    #2,d0            ;d0 = offset to plane #1
  588.     move.w    d0,d5
  589.     lsr.w    #1,d5
  590.     subq.w    #1,d5            ;d5 = # of words in line (-1)
  591. *
  592.     move.w    d0,d1
  593.     add.w    d1,d1            ;d1 = offset to plane #2
  594.     move.w    d1,d2
  595.     add.w    d0,d2            ;d2 = offset to plane #3
  596.     move.w    -14(a2),d3        ;get form height
  597.     bra    grset1i
  598. grset1:
  599.     move.w    d5,d6
  600. grset2:
  601.     move.w    (a5,d2.w),d4
  602.     or.w    (a5,d1.w),d4
  603.     or.w    (a5,d0.w),d4
  604.     or.w    (a5)+,d4
  605.     move.w    d4,(a0)+
  606.     dbra    d6,grset2    
  607.     adda.w    d2,a5
  608. grset1i:
  609.     dbra    d3,grset1
  610.     addq.w    #1,intrinscnt    ;advance count of intrinsic brushes
  611.     bra    scanend
  612. *
  613. *  Do something here if error
  614. *
  615. bad_nf:
  616.     move.w    #0,d0        ;b15-b14 = 0 if file not found
  617.     bra    badf
  618. bad_read:
  619.     move.w    #$4000,d0    ;b15-b14 = 1 if read error
  620.     bra    badf
  621. bad_notiff:
  622.     move.w    #$8000,d0    ;b15-b14 = 2 if not .iff file format
  623.     bra    badf
  624. bad_not16:
  625.     move.w    #$c000,d0    ;b15-b14 = 3 if not in 16-color mode
  626. badf:
  627.     or.w    brushnbr,d0    ;tell 'em which brush #
  628.     bsr    senderr
  629. *
  630. *  Mono mask now ready
  631. *
  632. scanend:
  633.     move.b    (a1)+,d0        ;search for next numeric character
  634.     beq    endblist        ;br if reached list terminator
  635.     cmpi.b    #cr,d0
  636.     bne    scanend            ;scan til end of this line
  637.  
  638.     subq.l    #1,a1
  639.     bra    rdbrush    
  640. *
  641. *  we finished building all intrinsic brushes..
  642. *    
  643. endblist:
  644.     move.w    intrinscnt,maxintrins    ;** .9e
  645.     move.l    #dstrng,dstrngend    ;init derived string block
  646. *
  647. *   a0 -> next graphics build area
  648. *
  649.     bsr    SubTile            ;add subdivided tiles to intrinsic
  650. *
  651. *
  652.     move.l    a0,library        ;this is where our next buffer starts
  653. *
  654. *
  655. **************************************
  656. *
  657. *  Let's build a library
  658. *
  659. *  the form width should be the widest of (150, maxbshwid, or 
  660. *    default library window width)
  661. *
  662.     move.w    wstruct+lwidth,d7
  663.     move.w    maxbrshwid,d0
  664.     add.w    #8,d0
  665.     cmp.w    d0,d7
  666.     bcc    widenuf
  667.     move.w    d0,d7
  668. widenuf:
  669.     cmpi.w    #150,d7        ;make library form 150 or wider
  670.     bcc    widenuf0
  671.     move.w    #150,d7
  672. widenuf0:
  673.     move.w    d7,wstruct+l_wid    ;library world is wide enough
  674.                     ;for widest brush
  675.     lea    bshblock,a0
  676.     move.l    #libblock,a1
  677. *
  678.     moveq    #0,d7        ;d7 = brush #
  679.     moveq    #4,d0        ;d0 = left edge start position
  680.     move.w    wstruct+l_wid,d1    ;d1 = right edge max
  681.     clr.w    maxhite
  682.     move.w    d0,d2        ;d2 = current xpos
  683.     move.w    d0,d3        ;d3 = current ypos
  684. lookilp:
  685.     move.w    (a0),d4        ;get width of next brush
  686.     bne    gotone
  687.     bra    donext    
  688. gotone:
  689.     add.w    d2,d4    ;find right edge of this object
  690.     cmp.w    d1,d4    ;did we exceed rightmost world position? 
  691.     bcs    samerow
  692.     move.w    d0,d2    ;start a new row
  693.     add.w    maxhite,d3
  694.     addq.w    #4,d3
  695.     clr.w    maxhite
  696. samerow:
  697.     move.w    2(a0),d5    ;get height of object
  698.     cmp.w    maxhite,d5
  699.     bcs    notmax
  700.     move.w    d5,maxhite
  701. notmax:
  702.     move.w    d7,(a1)+    ;record the object #
  703.     clr.w    (a1)+        ;0 -> special effect (not defined yet)
  704.     move.w    d2,(a1)+    ;record xpos
  705.     move.w    d3,(a1)+    ;record ypos
  706.     add.w    (a0),d2        ;advance xpos
  707.     addq.w    #4,d2
  708. *
  709. donext:
  710.     adda.w    #16,a0
  711.     addq.w    #1,d7
  712.     cmp.w    maxintrins,d7
  713.     bls    lookilp
  714. *
  715.     add.w    maxhite,d3
  716.     add.w    #10,d3
  717.     move.w    d3,wstruct+l_hite    ;save height of library
  718. *
  719.     move.l    a1,liblkptr
  720.     move.l    a1,wblkptr    ;fake it out
  721.     move.w    #-1,worldtile    ;so we don't get a floor
  722. *
  723.     move.w    maxintrins,maxderivd    ;start derived after max-intrinsic
  724. *
  725.     bsr    makelib        ;build the library
  726. *
  727.     move.w    w_wrap,d0    ;get wrap of library
  728.     mulu    wstruct+l_hite,d0
  729.     add.l    library,d0
  730.     move.l    d0,world    ;here is the start of the world
  731. *
  732. *  Base ptr for worldmsk is generated & returned by makewld    
  733. *
  734.     move.l    #worldblk,a0
  735.     move.l    a0,wblkptr
  736.     move.l    a0,wblkcur
  737. *
  738. *
  739.     bsr    makewld        ;now build an empty world
  740.     bsr    makemsk        ;with mask
  741. *
  742.     move.l    derivdgraf,derivdend    ;indicate no derived grafix used
  743.     move.l    #derivblk,derblkend    ;init derivation descriptor
  744. *
  745. *
  746. *dispdone:
  747.     lea    palblock,a0
  748.     move.w    firstpal,d0
  749.     bmi    nopal
  750.     lsl.w    #5,d0
  751.     adda.w    d0,a0
  752. *
  753.     move.l    a0,-(sp)
  754.     move.w    #6,-(sp)    ;set the palette
  755.     trap    #14
  756.     addq.l    #6,sp
  757. *
  758. nopal:
  759. *
  760. *  Init xoffset/yoffset for Cary
  761. *
  762.     lea    world_ID,a0
  763.     move.w    #WORLD,d0
  764.     bsr    DaveName
  765. *
  766.     move.l    BshEnvir,a0    ;assume no BRUSH.LST
  767.     tst.w    blistexist
  768.     beq    bListno
  769.     lea    BshListPath,a0
  770. bListno:
  771.     move.w    #LIBRARY,d0
  772.     bsr    DaveName    ;tell 'em where we got the library
  773. *
  774.     moveq    #Arrow,d0
  775.     bsr    Graf_Mouse    ;return to an Arrow cursor
  776. *
  777.     bsr    Back2G        ;get cluster status for curbrush
  778. *
  779.     move.w    intrinscnt,d0    ;tell Cary how many intrinsic brushes we loaded
  780.     beq    noerror
  781.     cmp.l    #undbuf+4096,undform    ;check for brush errors
  782.     beq    noerror
  783.     move.w    #-1,d0        ;indicate errors    
  784. noerror:
  785.     movem.l    (sp)+,a0-a6/d1-d7
  786.     rts            ;we're done with the init
  787. *
  788. *
  789. *    Write out error report file
  790. *
  791. SaveErr:
  792. *
  793. *    Create error file
  794. *
  795.     lea    BshListPath,a0    ;make an error report in this envir
  796.     move.l    a0,a1
  797. s_err0:    
  798.     tst.b    (a1)+
  799.     bne    s_err0
  800. *
  801.     subq.l    #4,a1
  802.     move.l    a1,a2
  803.     move.b    #"E",(a1)+
  804.     move.b    #"R",(a1)+
  805.     move.b    #"R",(a1)+    ;make error string
  806. *
  807.     move.l    a2,-(sp)
  808. *
  809.     clr.w    -(sp)
  810.     move.l    a0,-(sp)
  811.     move.w    #$3c,-(sp)
  812.     trap    #1        ;create error file
  813.     addq.l    #8,sp
  814. *
  815.     move.l    (sp)+,a2
  816.     move.b    #"L",(a2)+
  817.     move.b    #"S",(a2)+
  818.     move.b    #"T",(a2)+    ;restore BRUSH.LST string
  819. *
  820.     move.w    d0,handle
  821.     bmi    s_errerr    ;br if error writing error report
  822. *
  823. *  Start pumping out a report
  824. *        
  825.     move.l    undform,a2    ;get end of error report before we clobber
  826. *
  827.     move.l    #undbuf,undform    ;init file write routine
  828. *
  829. *
  830.     lea    errmsg0,a1    ;"Error Report"
  831.     bsr    strngout
  832. *
  833. *  Search for BRUSH.LST read error
  834. *
  835.     lea    undbuf+4096,a0    ;get start of error report
  836. blstrlp:
  837.     cmp.l    a2,a0
  838.     bcc    blstrq
  839.     cmpi.w    #-1,(a0)+    ;is it special case?
  840.     bne    blstrlp
  841. *
  842.     lea    errmsg1,a1    ;"Read Error on BRUSH.LST"
  843.     bsr    strngout
  844. blstrq:
  845. *    
  846. *  Search for next class of error
  847. *
  848.     moveq    #0,d3        ;start with file not found
  849. classer:
  850.     lea    undbuf+4096,a0    ;get start of error report
  851.     moveq    #0,d2        ;indicate no error of this class found
  852. classer1:
  853.     cmp.l    a2,a0
  854.     bcc    classq
  855. *
  856.     move.w    (a0)+,d0
  857.     move.w    d0,d1
  858.     andi.w    #$c000,d1
  859.     cmp.w    d3,d1        ;our class of error?
  860.     bne    classer1    ;br if not
  861. *
  862.     tst.w    d2        ;have we put out header yet?
  863.     bne    classer2    ;br if so
  864. *
  865.     moveq    #-1,d2        ;tell 'em we have now
  866. *
  867.     rol.w    #4,d3        ;get index for string ptrs
  868.     lea    errptrs,a1
  869.     move.l    (a1,d3.w),a1
  870.     bsr    strngout
  871.     ror.w    #4,d3
  872. *    
  873. classer2:
  874.     andi.w    #$3fff,d0    ;
  875.     bsr    decout        ; put out the number
  876.     move.b    #" ",d0
  877.     bsr    charwri
  878.     bra    classer1
  879. *
  880. *  Advance to next class
  881. *
  882. classq:
  883.     add.w    #$4000,d3
  884.     bcc    classer
  885. *
  886.     lea    undbuf,a0
  887.     move.l    undform,d0
  888.     sub.l    a0,d0
  889.     beq    exactbuf1
  890. *
  891. *  write out partial last buffer
  892. *
  893.     move.l    a0,-(sp)
  894.     move.l    d0,-(sp)
  895.     move.w    handle,-(sp)
  896.     move.w    #$40,-(sp)
  897.     trap    #1        ;write next buffer full of file
  898.     adda.w    #12,sp
  899.     cmp.l    -8(sp),d0
  900. *
  901. exactbuf1:
  902.     move.w    handle,-(sp)
  903.     move.w    #$3e,-(sp)
  904.     trap    #1            ;close file
  905.     addq.l    #4,sp
  906. *
  907.     moveq    #0,d0
  908. s_errerr:
  909.     rts
  910. *
  911. *
  912. *
  913. *  AES call to Cary's Event_Multi
  914. *
  915. *  Returns:    d0.w - mouse xpos
  916. *        d1.w - mouse ypos
  917. *        d2.w - keyboard control key bits
  918. *        d3.w - mouse buttons
  919. *
  920. *   
  921. getmouse:
  922.     movem.l    a0-a3,-(sp)
  923.     bsr    DaveEvnt
  924.     movem.w    Mrets,d0-d3
  925.     exg    d2,d3
  926.     move.w    Key,d4
  927.     clr.w    Key
  928.     movem.l    (sp)+,a0-a3
  929.     rts
  930. *
  931. *  Get cookie jar info
  932. *
  933. getcook:
  934.     moveq    #0,d1
  935.     move.l    $5a0,d0
  936.     beq    cookxit        ;exit if no cookie jar, must be vanilla ST
  937.     move.l    d0,a0        ;else, we got a cookie jar
  938. cooklp:
  939.     move.l    (a0)+,d0
  940.     beq    cookxit        ;exit if past last entry
  941.     cmpi.w    #2,d1        ;did we get our 2 values?
  942.     bcc    cookxit        ;br if so
  943. *
  944.     cmpi.l    #"_CPU",d0    ;check cpu entry
  945.     bne    cooknxt
  946.     move.l    (a0),d0
  947.     cmpi.w    #30,d0
  948.     bne    cooknxt1
  949.     move.w    #-1,cexist    ;tell 'em cache exists
  950.     bra    cooknxt1
  951. cooknxt:
  952.     cmpi.l    #"_VDO",d0    ;check video shifter type
  953.     bne    cooknxt1
  954.     addq.w    #1,d1
  955.     move.w    (a0),vidtyp
  956. cooknxt1:
  957.     addq.l    #4,a0
  958.     bra    cooklp    
  959. cookxit:
  960.     rts
  961. *
  962. *
  963. ;
  964. ;  take binary value in d0 and put out as decimal string ->a1
  965. ;
  966. ;  left justify with no right space padding
  967. *  5-char max
  968. *
  969. decit:
  970.     lea    decitab(pc),a0
  971.     moveq    #4,d1
  972. decit0:
  973.     moveq    #0,d2
  974. decit1:
  975.     andi.l    #$ffff,d0
  976.     divu    (a0)+,d0
  977.     tst.w    d2        ;have we put out leading non-zero yet?
  978.     bne    decit2        ;br if so
  979.     tst.w    d1        ;is this the last chance to put out a char?
  980.     beq    decit2        ;br if so--put out at least 1 char
  981.     tst.w    d0        ;else, is it a leading zero?
  982.     beq    decit3
  983. decit2:
  984.     moveq    #-1,d2
  985.     add.b    #"0",d0
  986.     move.b    d0,(a1)+
  987. decit3:
  988.     swap    d0
  989.     dbra    d1,decit1
  990.     rts
  991. ;
  992. ;  take signed binary value in d0 and put out as decimal string ->a1
  993. *  left justify with right space padding, 4-chars
  994. *
  995. decitpad:
  996.     move.l    a0,-(sp)
  997.     lea    decitab(pc),a0
  998.     moveq    #4,d1        ;# of divisions -1
  999.     moveq    #5,d3        ;# of trailing spaces
  1000.     moveq    #0,d2        ;leading zero indicator
  1001.     tst.w    d0
  1002.     bpl    decitp1
  1003.     neg.w    d0
  1004.     move.b    #"-",(a1)+
  1005.     subq.w    #1,d3
  1006. decitp1:
  1007.     andi.l    #$ffff,d0
  1008.     divu    (a0)+,d0
  1009.     tst.w    d2        ;have we put out leading non-zero yet?
  1010.     bne    decitp2        ;br if so
  1011.     tst.w    d1        ;is this the last chance to put out a char?
  1012.     beq    decitp2        ;br if so--put out at least 1 char
  1013.     tst.w    d0        ;else, is it a leading zero?
  1014.     beq    decitp3
  1015. decitp2:
  1016.     moveq    #-1,d2
  1017.     add.b    #"0",d0
  1018.     move.b    d0,(a1)+
  1019.     subq.w    #1,d3
  1020. decitp3:
  1021.     swap    d0
  1022.     dbra    d1,decitp1
  1023.     bra    decitp5
  1024. decitp4:
  1025.     move.b    #" ",(a1)+
  1026. decitp5:
  1027.     dbra    d3,decitp4
  1028.     move.l    (sp)+,a0
  1029.     rts
  1030. *
  1031. *
  1032. *
  1033. *
  1034. DaveExit:
  1035.     btst    #0,butt
  1036.     bne    dxit
  1037.     bsr    switchpal    ;restore original palette
  1038. dxit:
  1039.     tst.w    blistexist    ;does a brush list file exist?
  1040.     bne    dxit1        ;br if so
  1041. *
  1042.     lea    BshListPath,a0
  1043.     bsr    save_brush    ;save one if none exists
  1044. dxit1:
  1045.     rts
  1046. *
  1047. *
  1048. *       
  1049. * pathname in A0
  1050. *       
  1051. save_brush:
  1052.     lea    brushlst,a1    ;buffer beginning in a1
  1053.     move.l    a1,-(sp)
  1054. s_b1:
  1055.     tst.b    (a1)+
  1056.     bne    s_b1
  1057.     move.l    a1,d1
  1058.     move.l    (sp)+,a1
  1059.     sub.l    a1,d1        ;buffer length in d1
  1060.     subq.l    #1,d1        ;don't need to save null terminator
  1061. *
  1062. *  open file for write
  1063. *
  1064. savefile:
  1065.     movem.l    d1/a1,-(sp)
  1066. *
  1067.     move.w    #0,-(sp)
  1068.     move.l    a0,-(sp)
  1069.     move.w    #$3c,-(sp)    ;create file
  1070.     trap    #1
  1071.     addq.l    #8,sp
  1072. *
  1073.     movem.l    (sp)+,d1/a1
  1074. *
  1075.     move.w    d0,handle
  1076.     bpl    sbshgood
  1077. *
  1078. sbsherr:
  1079.     moveq    #-1,d0        ;error exit
  1080.     rts
  1081. *
  1082. *
  1083. sbshgood:
  1084.     move.l    a1,-(sp)
  1085.     move.l    d1,-(sp)
  1086.     move.w    handle,-(sp)
  1087.     move.w    #$40,-(sp)
  1088.     trap    #1            ;write body of file
  1089. *
  1090.     adda.w    #12,sp
  1091.     cmp.l    -8(sp),d0
  1092.     bne    sbsherr
  1093. *
  1094.     move.w    handle,-(sp)
  1095.     move.w    #$3e,-(sp)
  1096.     trap    #1            ;close file
  1097.     addq.l    #4,sp
  1098. *
  1099.     moveq    #0,d0            ;return "good"
  1100.     rts
  1101. *
  1102. *********************************************************
  1103. *
  1104. *    Load a text file describing the world
  1105. *
  1106. *       
  1107. * pathname in A0
  1108. *        
  1109. load_world:
  1110.     movem.l    a0-a6/d0-d7,-(sp)
  1111. *
  1112.     move.l    a0,-(sp)
  1113.     lea    world_ID,a1
  1114.     move.l    a1,a2
  1115. l_w0:
  1116.     move.b    (a0)+,(a1)+
  1117.     bne    l_w0
  1118.     move.l    a2,a0
  1119.     move.w    #WORLD,d0
  1120.     bsr    DaveName
  1121.     move.l    (sp)+,a0
  1122. *
  1123.     move.w    #0,-(sp)
  1124.     move.l    a0,-(sp)
  1125.     move.w    #$3d,-(sp)    ;open file for read
  1126.     trap    #1
  1127.     addq.l    #8,sp
  1128. *
  1129.     move.w    d0,handle
  1130.     bpl    filether
  1131. *
  1132. *  exit if read error
  1133. *
  1134. rderr:
  1135.     movem.l    (sp)+,a0-a6/d0-d7
  1136.     moveq    #-1,d0
  1137.     rts
  1138. *
  1139. filether:
  1140.     move.l    world,a4 ;temporarily stuff world data where grafix is
  1141.     bsr    parsetxt    ;get machine readable in buffer at a4
  1142. *
  1143. *    a4 -> end of buffer
  1144. *
  1145.     move.l    world,a0
  1146.     sub.l    a0,a4        ;get length of machine readable
  1147.     movem.l    a0/a4,-(sp)    ;save length
  1148.     movem.w    (a0),d4-d5
  1149. fover0a:
  1150.     bsr    adjsize        ;adjust buffers for this world size
  1151.     beq    fover0        ;br if happy with size
  1152. *
  1153.     movem.w    d4-d5,-(sp)
  1154.     bsr    TooBigRam    ;ask user what to do
  1155.     movem.w    (sp)+,d4-d5
  1156.     tst.w    d0
  1157.     bne    fover0a        ;br if user says clip world
  1158.     addq.l    #8,sp        ;else, user says give up
  1159.     bra    fover1        ;clean up stack & skip over stuff
  1160. fover0:
  1161.     movem.l    (sp)+,a0/a4    ;reget base & length
  1162.     movem.w    d4-d5,(a0)    ;save new (if any) world dimensions
  1163. *
  1164. *  copy read in world to world data area
  1165. *
  1166. *   we still need to perform certain translations here
  1167. *
  1168.     move.l    a4,d0        ;get length
  1169.     lsr.w    #1,d0        ;in words
  1170.     lea    wfile,a1
  1171.     add.l    a1,a4
  1172.     move.l    (a0)+,(a1)+    ;copy width & height
  1173.     subq.w    #2,d0        ;adjust count
  1174. *
  1175.     moveq    #8,d2        ;assume new style 10 word header
  1176.     btst.l    #0,d0        ;if odd, it must be old 9 word header
  1177.     beq    fover2a
  1178.     moveq    #7,d2        ;it's old header
  1179.     clr.w    (a1)+        ;put in 0th palette
  1180. fover2a:
  1181.     move.w    d2,d1
  1182.     bra    fover2i
  1183. fover2:
  1184.     move.w    (a0)+,(a1)+    ;copy remaining 7 or 8 word header
  1185. fover2i:
  1186.     dbra    d1,fover2    ;
  1187. *
  1188.     sub.w    d2,d0        ;adjust file length
  1189.     bls    foverover    ;exit if only header
  1190. *
  1191. *  now read in each sprite item
  1192. *
  1193.     move.w    curbrush,-(sp)    ;we need to use curbrush
  1194. fover3:
  1195.     move.w    (a0)+,d1    ;brush #
  1196.     move.w    d1,curbrush
  1197.     move.w    (a0)+,d2    ;effects
  1198. *
  1199.     cmpi.w    #FillRect,d2    ;check for special 6 word item
  1200.     bne    fover3a
  1201. *
  1202. *  We gotta filled rectangle here..
  1203. *
  1204.     move.w    d1,rectcol    ;save color
  1205.     movem.w    (a0)+,d3-d4    ;pick-up hpos/vpos
  1206.     movem.w    (a0),d5-d6
  1207.     movem.w    d5-d6,rectwid
  1208.     cmp.w    new_wid,d3
  1209.     bge    holeclp        ;exit if wholly off the world
  1210.     cmp    new_hite,d4
  1211.     bge    holeclp
  1212.     add.w    d3,d5
  1213.     ble    holeclp
  1214.     add.w    d4,d6
  1215.     ble    holeclp
  1216. *
  1217. *  search for this rectangle already defined
  1218. *
  1219.     lea    bshblock,a2
  1220.     move.w    maxintrins,d7    ;start searching after intrinsics (deriveds)
  1221.     bra    rectelpi
  1222. rectelp:
  1223.     lsl.w    #4,d7
  1224.     tst.w    14(a2,d7.w)    ;are we a filled rectangle?
  1225.     bne    rectelpx
  1226.     movem.w    (a2,d7.w),d1-d2    ;get width & height
  1227.     cmp.w    rectwid,d1
  1228.     bne    rectelpx
  1229.     cmp.w    recthite,d2
  1230.     bne    rectelpx
  1231.     move.w    12(a2,d7.w),d1
  1232.     cmp.w    rectcol,d1
  1233.     bne    rectelpx
  1234. *
  1235. *  we found a pre-existing rect fill we can use..
  1236. *
  1237.     lsr.w    #4,d7
  1238.     move.w    d7,(a1)+    ;we save as a brush
  1239.     bra    prerect        ;no need to create a new one
  1240. rectelpx:
  1241.     lsr.w    #4,d7
  1242. rectelpi:
  1243.     addq.w    #1,d7
  1244.     cmp.w    maxderivd,d7    ;have we searched all pre-existing?
  1245.     ble    rectelp        ;br if not
  1246.  
  1247.     move.w    maxderivd,d7
  1248.     addq.w    #1,d7
  1249. *
  1250.     movem.l    a0-a1/d0/d3-d4,-(sp)
  1251.     bsr    genarect    ;build this filled rectangle
  1252.     move.l    a1,dstrngend
  1253.     add.l    #18,derblkend
  1254.     addq.w    #1,maxderivd
  1255.     movem.l    (sp)+,a0-a1/d0/d3-d4
  1256.     move.w    maxderivd,(a1)+    ;save brush #
  1257. prerect:
  1258.     clr.w    (a1)+        ;effect not used
  1259.     move.w    d3,(a1)+
  1260.     move.w    d4,(a1)+
  1261.     subq.w    #2,d0
  1262.     bra    holeclp
  1263. *
  1264. *  Normal 4-word item needs processing..
  1265. *
  1266. fover3a:
  1267. *
  1268. *  see if this object is wholly clipped off world
  1269. *
  1270.     movem.w    (a0),d3-d4    ;pick-up hpos/vpos
  1271.     cmp.w    new_wid,d3
  1272.     bge    holeclp        ;br if wholly clipped off to right
  1273.     cmp.w    new_hite,d4
  1274.     blt    notclp        ;br if not wholly off bottom
  1275. holeclp:
  1276.     addq.l    #4,a0
  1277.     bra    fover6        ;skip this clipped item    
  1278. notclp:
  1279.     lsl.w    #4,d1
  1280.     lea    bshblock,a2
  1281.     add.w    (a2,d1.w),d3    ;add width
  1282.     ble    holeclp        ;br if wholly off left
  1283.     add.w    2(a2,d1.w),d4    ; & height for clip check
  1284.     ble    holeclp        ;br if wholly off top
  1285. *
  1286.     move.w    d2,d3
  1287.     andi.w    #Hflip,d3    ;do we need hflip?
  1288.     beq    fover4
  1289.     movem.l    a0-a1/a4/d0-d3,-(sp)
  1290.     bsr    makehflp
  1291.     movem.l    (sp)+,a0-a1/a4/d0-d3
  1292. fover4:
  1293.     move.w    d2,d3
  1294.     andi.w    #Vflip,d3    ;do we need vflip
  1295.     beq    fover5
  1296.     movem.l    a0-a1/a4/d0-d3,-(sp)
  1297.     bsr    makevflp
  1298.     movem.l    (sp)+,a0-a1/a4/d0-d3
  1299. fover5:
  1300.     move.w    d2,d3
  1301.     andi.w    #Alternate,d3    ;do we need alternate
  1302.     beq    fover5a
  1303.     movem.l    a0-a1/a4/d0-d3,-(sp)
  1304.     bsr    makealt
  1305.     movem.l    (sp)+,a0-a1/a4/d0-d3
  1306. fover5a:
  1307.     move.w    curbrush,(a1)+    ;use derived brush
  1308.     andi.w    #$ffff-(Hflip+Vflip+Alternate),d2
  1309.     move.w    d2,(a1)+    ;save effects
  1310.     move.l    (a0)+,(a1)+    ;copy hpos/vpos
  1311. fover6:
  1312.     subq.w    #4,d0
  1313.     bhi    fover3        ;go for all items
  1314. *
  1315. *  we're done--restore curbrush
  1316. *
  1317.     move.w    (sp)+,curbrush
  1318. foverover:
  1319.     move.l    a1,wblkptr    ;save end of world ptr
  1320.     move.l    a1,wblkcur    ;set for highest priority
  1321. *
  1322. *  now let's clean up the bridges..
  1323. *
  1324.     lea    worldblk,a0    ;start at begining of world data
  1325.     bra    bridglpi
  1326. bridglp:
  1327.     movem.w    (a0)+,d0-d3    ;pick up next item
  1328.     move.w    d1,d4
  1329.     andi.w    #Hbridge+Vbridge,d4    ;check for a bridge
  1330.     bne    bridg0            ;br if bridge here
  1331. *    move.l    #$80008000,mousex    ;indicate previous was intrinsic
  1332. *    move.l    #$80008000,tempbuf+22
  1333.     movem.w    d2-d3,tempbuf+22
  1334.     bra    bridglpi
  1335. *
  1336. *  we gotta bridge..
  1337. *
  1338. bridg0:
  1339.     suba.w    #16,a0
  1340.     lea    24(a0),a4
  1341.     move.l    -4(a4),mousex
  1342. *
  1343.     move.w    curbrush,-(sp)
  1344.     movem.l    a0-a1,-(sp)
  1345.     move.l    d1,-(sp)
  1346.     bsr    genbridg
  1347.     move.l    (sp)+,d7
  1348. *
  1349.     move.l    (sp),a4        ;get previous "a0"
  1350.     andi.w    #Hbridge,d7
  1351.     bne    dohb
  1352. *
  1353.     move.w    14(a4),d7    ;get vpos of bridge item
  1354.     cmp.w    22(a4),d7    ;is "bridge-to" item below it?
  1355.     bge    dovbx    ;br if not--we'll use previous intrinsic vpos
  1356.     move.w    #$8000,tempbuf+24    ;else do normal
  1357. dovbx:
  1358.     bsr    vstretch
  1359.     bra    afterdo
  1360. dohb:
  1361.     move.w    12(a4),d7
  1362.     cmp.w    20(a4),d7
  1363.     bge    dohbx
  1364.     move.w    #$8000,tempbuf+22
  1365. dohbx:
  1366.     move.w    tempbuf+22,tempbuf+24    ;put hpos in correct place
  1367.     bsr    hstretch
  1368. afterdo:
  1369.     bsr    reclumsk
  1370.     
  1371.     movem.l    (sp)+,a0-a1
  1372.     move.l    mousex,tempbuf+22 ;get last hpos/vpos used for this bridge
  1373.     move.w    curbrush,d0
  1374.     move.w    d0,(a0)+
  1375.     clr.w    (a0)+
  1376. *    addq.l    #4,a0        ;skip over previous h/v
  1377.     move.l    xmin_clust,d0
  1378.     move.l    d0,(a0)+
  1379.     lea    16(a0),a2
  1380.     move.l    a0,-(sp)
  1381.     bra    afterlpi
  1382. afterlp:
  1383.     move.l    (a2)+,(a0)+
  1384. afterlpi:
  1385.     cmp.l    a1,a2
  1386.     bcs    afterlp
  1387. *
  1388.     move.l    (sp)+,a0
  1389.     suba.w    #16,a1        ;remove 2 items
  1390. *    
  1391.     move.l    a1,wblkptr    ;save new end of world ptr
  1392.     move.l    a1,wblkcur    ;set for highest priority
  1393. *    
  1394.     move.w    (sp)+,curbrush
  1395. bridglpi:
  1396.     cmp.l    a1,a0
  1397.     bcs    bridglp
  1398. *
  1399. *   now build world just read in..
  1400. *
  1401.     lea    wstruct,a5
  1402.     move.w    new_wid,w_wid(a5)
  1403.     move.w    new_hite,w_hite(a5)
  1404. *
  1405. fover1:
  1406.     bsr    makewld
  1407.     bsr    makemsk
  1408.     move.w    worldpal,firstpal
  1409.     bsr    switch1            ;switch to new palette
  1410.     movem.l    (sp)+,a0-a6/d0-d7
  1411. *
  1412.     rts
  1413. *
  1414. *
  1415. ********************************************
  1416. *
  1417. *  Routine to parse input text file into world/cluster data
  1418. *
  1419. *    Entry:
  1420. *        Getchar input file routine must be set-up
  1421. *
  1422. *        a4 -> buffer to place input data
  1423. *
  1424. *    Exit:
  1425. *        a4 -> end of buffer as read in
  1426. *
  1427. parsetxt:
  1428.     move.l    #undbuf+4096,a0
  1429.     move.l    a0,undform    ;use this for a read buffer ptr
  1430.     move.l    a0,undhite    ;save as end-of-buffer ptr
  1431.     clr.w    undwid        ;save as end-of-file indicator
  1432. *
  1433. *
  1434.     lea    bshblock,a2
  1435. worldlp:
  1436. crwait:
  1437.     bsr    getchar        ;get next file char
  1438.     bne    fileover    ;br if no more chars
  1439. crwait1:
  1440.     cmpi.b    #cr,d0        ;did we get a <CR>
  1441.     bne    crwait        ;br if no
  1442. *
  1443. *  we're at the beginning of a line..
  1444. *
  1445. prwait:    
  1446.     bsr    getchar
  1447.     bne    fileover
  1448.     cmpi.b    #" ",d0        ;wait for printing char
  1449.     bcs    prwait        ;br if not printing char
  1450. *
  1451.     cmpi.b    #"*",d0
  1452.     beq    crwait        ;br if we're a comment line
  1453. *
  1454.     cmpi.b    #";",d0        ;are we a comment line?
  1455.     beq    crwait        ;if so, wait for next <CR>
  1456.     bra    chkdot
  1457. *
  1458. *  wait for .byte or .word
  1459. *
  1460. dotwait:
  1461.     bsr    getchar
  1462.     bne    fileover
  1463. chkdot:
  1464.     cmpi.b    #".",d0
  1465.     beq    pword        ;this could be .word or .byte
  1466. *
  1467.     cmpi.b    #"*",d0
  1468.     beq    crwait        ;br if we're a comment line
  1469. *
  1470.     cmpi.b    #";",d0        ;are we a comment line?
  1471.     beq    crwait        ;if so, wait for next <CR>
  1472. *
  1473.     cmpi.b    #cr,d0
  1474.     beq    prwait
  1475.     bra    dotwait
  1476. *
  1477. *   we got a dot--maybe its .word or .byte
  1478. *
  1479. pword:    
  1480.     bsr    getchar
  1481.     bne    fileover
  1482.     cmpi.b    #"a",d0        ;check for lower case
  1483.     bcc    pw0
  1484.     addi.b    #$20,d0        ;force lower case
  1485. pw0:
  1486.     moveq    #0,d1
  1487. *
  1488.     cmpi.b    #"b",d0        ;are we .byte?
  1489.     beq    pw1
  1490.     moveq    #wordname-bytename,d1
  1491.     cmpi.b    #"w",d0        ;are we .word?
  1492.     bne    crwait        ;br to skip this line if confused
  1493. pw1:
  1494.     move.b    bytename(pc,d1.w),d2
  1495.     beq    pw3        ;br if we got a good .word/.byte
  1496. *
  1497.     bsr    getchar
  1498.     bne    fileover
  1499.     cmpi.b    #"a",d0        ;check for lower case
  1500.     bcc    pw2
  1501.     addi.b    #$20,d0        ;force lower case
  1502. pw2:
  1503.     cmp.b    d2,d0
  1504.     bne    crwait        ;br if we got bad
  1505.     addq.w    #1,d1
  1506.     bra    pw1
  1507. *
  1508. bytename:
  1509.     dc.b    "yte",0
  1510. wordname:
  1511.     dc.b    "ord",0
  1512. *
  1513. *    Got .word or .byte
  1514. *
  1515. pw3:
  1516.     bsr    getchar
  1517.     bne    fileover
  1518. *
  1519.     cmpi.b    #";",d0        ;comment?
  1520.     beq    crwait
  1521.     cmpi.b    #"*",d0
  1522.     beq    crwait
  1523. *
  1524.     cmpi.b    #cr,d0
  1525.     beq    prwait        ;br if reached end of line
  1526. *
  1527.     cmpi.b    #" ",d0        ;check for tab or space
  1528.     bls    pw3        ;eat sub-spaces
  1529. *
  1530. *  we got a number or a symbol
  1531. *
  1532. *  is it numeric?
  1533. *
  1534. chknum:
  1535.     moveq    #0,d2        ;clear numeric accumulator
  1536.     moveq    #-1,d1        ;assume negative
  1537.     cmpi.b    #"-",d0        ;minus means numeric
  1538.     beq    donum
  1539.     moveq    #0,d1        ;it could be positive
  1540.     cmpi.b    #"0",d0
  1541.     bcs    crwait        ;we don't take no punctuation
  1542.     cmpi.b    #"9",d0
  1543.     bls    donum1        ;br if we're a Number
  1544. *
  1545. *  could be a symbol--copy to local buffer and search
  1546. *
  1547.     lea    brshfile,a3    ;use this as local buffer
  1548.     move.b    d0,(a3)+
  1549. symloop:
  1550.     bsr    getchar
  1551.     bne    schk1        ;use this as symbol terminator
  1552.     cmpi.b    #" ",d0        ;are we a terminator?
  1553.     bls    schk
  1554.     move.b    d0,(a3)+
  1555.     bra    symloop
  1556. schk1:
  1557.     moveq    #0,d0
  1558. schk:
  1559.     clr.b    (a3)
  1560. *   preserve d0 = terminating char
  1561. *
  1562. *  See if it is one of our key words
  1563. *
  1564.     lea    keywords,a5    ;start with list of keywords
  1565.     moveq    #0,d2
  1566.     lea    brshfile,a6
  1567. keyloop:
  1568.     move.l    a6,a3
  1569. kloop:
  1570.     move.b    (a5)+,d1    ;reached end of keyword string?
  1571.     beq    keyd        ;br if so--maybe a match
  1572.     cmp.b    (a3)+,d1
  1573.     beq    kloop        ;keep it up if we're matching 
  1574. *
  1575. *  no match--go for next
  1576. *
  1577. klp0:
  1578.     tst.b    (a5)+
  1579.     bne    klp0        ;scan to end of keyword string
  1580.     addq.l    #1,a5        ;skip symbol value
  1581.     tst.b    (a5)        ;at end of keyword list?
  1582.     bne    keyloop        ;br if more to check
  1583.     bra    checkobjs    ;else, check user loaded symbols
  1584. *
  1585. *  matched all keyword chars--gotta get a terminator
  1586. *
  1587. keyd:
  1588.     move.b    (a3)+,d1    ;this one must be a terminator
  1589.     cmpi.b    #"+",d1        ;or separator
  1590.     bne    klp1
  1591.     or.b    (a5)+,d2
  1592.     lea    keywords,a5
  1593.     move.l    a3,a6
  1594.     bra    keyloop
  1595. klp1:
  1596.     cmp.b    #" ",d1
  1597.     bhi    klp0        ;br if not terminator
  1598. *
  1599.     or.b    (a5)+,d2    ;or in the byte of symbol value
  1600.     cmpi.b    #$ff,d2        ;check for -1 (special case)
  1601.     bne    numdone        ;br if not
  1602.     ext.w    d2        ;else, hi byte must be -1 too
  1603.     bra    numdone        ;we got a number to stuff
  1604. *
  1605. checkobjs:
  1606.     moveq    #0,d2
  1607. symbloop:
  1608.     lea    brshfile,a3
  1609.     lsl.w    #4,d2
  1610.     move.l    8(a2,d2.w),a5    ;a5 -> next brush symbol string
  1611.     lsr.w    #4,d2
  1612.     move.l    a5,d1
  1613.     beq    symblp1        ;br if vacant brush entry
  1614. symblp0:
  1615.     move.b    (a3)+,d1
  1616.     beq    srcdone
  1617.     cmp.b    (a5)+,d1
  1618.     beq    symblp0
  1619. symblp1:
  1620.     addq.w    #1,d2
  1621.     cmp.w    maxintrins,d2
  1622.     bls    symbloop
  1623. *
  1624. *  we couldn't find the symbol
  1625. *
  1626.     moveq    #0,d2        
  1627.     bra    numdone
  1628. *
  1629. srcdone:
  1630.     move.b    (a5)+,d1    ;this one must be a terminator
  1631.     cmp.b    #" ",d1
  1632.     bhi    symblp1        ;br if not terminator
  1633. *
  1634.     bra    numdone        ;we got brush number
  1635. *
  1636. *  lets go for some numbers
  1637. *
  1638. donum:
  1639.     bsr    getchar
  1640.     bne    fileov        ;br here to save last partial number
  1641. donum1:
  1642.     cmpi.b    #" ",d0        ;terminator?
  1643.     bls    numdonex        ;br if so
  1644. *
  1645.     cmpi.b    #",",d0        ;separator?
  1646.     beq    numdonex
  1647. *
  1648.     subi.b    #"0",d0
  1649.     bcs    crwait        ;br if crazy data
  1650.     cmpi.b    #10,d0        ;is it decimal?
  1651.     bcc    crwait        ;just give up if not
  1652. *
  1653.     ext.w    d0
  1654.     mulu    #10,d2
  1655.     add.w    d0,d2        ;add to accum
  1656. *
  1657.     bra    donum
  1658. *
  1659. *
  1660. numdonex:
  1661.     tst.w    d1
  1662.     bpl    numdone
  1663.     neg.w    d2
  1664. numdone:
  1665.     move.w    d2,(a4)+    ;put in next entry
  1666.     cmpi.b    #",",d0        ;was this a separator?
  1667.     beq    pw3        ;br if separator
  1668.     bra    crwait1
  1669. *
  1670. fileov:
  1671.     move.w    d2,(a4)+
  1672. fileover:
  1673.     rts
  1674. *
  1675. *
  1676. *
  1677. *
  1678. *******************************************
  1679. *
  1680. *  Get next char in d0 from input buffer
  1681. *
  1682. getchar:
  1683.     move.l    a0,-(sp)
  1684.     move.l    undform,a0    ;get ptr
  1685.     cmp.l    undhite,a0    ;are we at end?
  1686.     bcs    getc2        ;br if not
  1687. *
  1688. * at end of buffer--are we at end of file
  1689. *
  1690.     tst.w    undwid        ;is that all there is?
  1691.     beq    getc0        ;br if more to come
  1692. *
  1693. *  else, we're done with file
  1694. *
  1695.     movea.l    (sp)+,a0
  1696.     rts            ;return NZ if done
  1697. *
  1698. *
  1699. getc0:
  1700. *
  1701. *  read in another buffers worth
  1702. *
  1703.     move.l    #undbuf,a0    ;start at the top
  1704.     movem.l    a0-a6/d1-d7,-(sp)
  1705.  
  1706.     move.l    a0,-(sp)
  1707.     move.l    #4096,-(sp)
  1708.     move.w    handle,-(sp)
  1709.     move.w    #$3f,-(sp)
  1710.     trap    #1        ;read next buffer full of world file
  1711.     adda.w    #12,sp
  1712.  
  1713.     movem.l    (sp)+,a0-a6/d1-d7
  1714.     tst.l    d0
  1715.     bmi    getc1        ;br on error
  1716.     cmp.l    #4096,d0    ;did we get a full buffer?
  1717.     beq    getc2        ;br if so
  1718. *
  1719. *  got a partial buffer--must be at end
  1720. *
  1721.     adda.w    d0,a0
  1722.     move.l    a0,undhite    ;save new end-of-buffer ptr
  1723.     move.w    #-1,undwid    ;set file done flag
  1724.     suba.w    d0,a0
  1725.     bra    getc2
  1726. *
  1727. *   Need to improve error handling here
  1728. *
  1729. getc1:
  1730.     move.l    (sp)+,a0
  1731.     moveq    #-1,d0        ;set NZ indication
  1732.     rts
  1733. *
  1734. getc2:
  1735.     move.b    (a0)+,d0
  1736.     move.l    a0,undform
  1737.     ori    #4,ccr        ;set Z flag
  1738.     move.l    (sp)+,a0
  1739.     rts
  1740. *
  1741. *
  1742. **********************************************************
  1743. *
  1744. *  Save a text file describing the world
  1745. *
  1746. * pathname in A0        
  1747. *
  1748. save_world:
  1749.     move.l    a0,-(sp)
  1750.     lea    world_ID,a1
  1751.     move.l    a1,d0
  1752. s_w0:
  1753.     move.b    (a0)+,(a1)+
  1754.     bne    s_w0
  1755. *
  1756.     move.l    d0,a0
  1757.     move.w    #WORLD,d0
  1758.     bsr    DaveName    
  1759. *
  1760.     move.l    (sp)+,a0
  1761. *
  1762. *
  1763.     move.w    #0,-(sp)
  1764.     move.l    a0,-(sp)
  1765.     move.w    #$3c,-(sp)    ;create file
  1766.     trap    #1
  1767.     addq.l    #8,sp
  1768. *
  1769.     move.w    d0,handle
  1770.     bmi    sbsherr        ;exit if file write error
  1771. *
  1772.     move.l    #undbuf,undform    ;use this for a write buffer
  1773. *
  1774.     lea    worldblk,a0    ;start with the world block
  1775.     lea    bshblock,a2
  1776. *
  1777.     lea    whead,a1    ;"--World List--"
  1778.     bsr    strngout
  1779. *
  1780.     move.w    wstruct+w_wid,d0
  1781.     bsr    decout
  1782. *
  1783.     lea    whead1,a1    ;"    ;World width in pixels"
  1784.     bsr    strngout
  1785. *
  1786.     move.w    wstruct+w_hite,d0
  1787.     bsr    decout
  1788. *
  1789.     lea    whead2,a1    ;"    ;World Height.."
  1790.     bsr    strngout
  1791. *
  1792. *  Now put out palette item
  1793. *
  1794.     move.w    firstpal,d0
  1795.     cmpi.w    #-1,d0
  1796.     bne    goodpalt
  1797.     moveq    #0,d0        ;just use first one
  1798. goodpalt:
  1799.     lsl.w    #4,d0
  1800.     move.l    8(a2,d0.w),a3
  1801. mopalit:
  1802.     move.b    (a3)+,d0
  1803.     cmpi.b    #" ",d0
  1804.     bls    palitdon
  1805.     bsr    charwri
  1806.     bra    mopalit
  1807. palitdon:
  1808.     lea    whead2a,a1    ;"    Palette.."
  1809.     bsr    strngout
  1810. *
  1811. *
  1812.     move.w    worldtile,d5    ;get the current floor tile
  1813.     bpl    gotfloor
  1814.     lea    nullname,a1
  1815.     bsr    strngout
  1816.     bra    symout1
  1817. *
  1818. *  Need to check for derived floor tile..
  1819. *
  1820. gotfloor:
  1821.     lsl.w    #4,d5
  1822.     move.l    8(a2,d5.w),a3    ;get symbol ptr
  1823. mosym1:
  1824.     move.b    (a3)+,d0
  1825.     cmpi.b    #" ",d0
  1826.     bls    symout1
  1827.     bsr    charwri
  1828.     bra    mosym1    
  1829. symout1:
  1830.     lea    floorxhd,a1    
  1831.     bsr    strngout
  1832. *
  1833.     lea    initxtile,a3
  1834.     tst.w    worldtile
  1835.     bpl    setIout
  1836.     moveq    #0,d0
  1837.     move.l    d0,(a3)
  1838.     move.l    d0,4(a3)
  1839.     move.l    d0,8(a3)
  1840. *    move.w    d0,12(a3)
  1841. setIout:
  1842.     move.w    (a3)+,d0
  1843.     bsr    decout
  1844.     moveq    #4,d3
  1845. parmsout:
  1846.     move.b    #",",d0
  1847.     bsr    charwri
  1848.     move.w    (a3)+,d0
  1849.     bsr    decout
  1850.     dbra    d3,parmsout
  1851. *
  1852. *  Get on with the object list
  1853. *
  1854.     lea    wwhead,a1
  1855.     bsr    strngout
  1856. *
  1857.     bra    objloopi
  1858. *
  1859. objloop:
  1860.     moveq    #0,d1
  1861.     moveq    #0,d2
  1862.     moveq    #0,d3
  1863.     movem.w    (a0),d4-d7    ;get brush data in regs
  1864.     bsr    brushout
  1865.     addq.l    #8,a0    
  1866. objloopi:
  1867.     cmp.l    wblkptr,a0
  1868.     bcs    objloop
  1869. *
  1870. *  all the objects are out--close this guy
  1871. *
  1872.     lea    undbuf,a0
  1873.     move.l    undform,d0
  1874.     sub.l    a0,d0
  1875.     beq    exactbuf
  1876. *
  1877. *  write out partial last buffer
  1878. *
  1879.     move.l    a0,-(sp)
  1880.     move.l    d0,-(sp)
  1881.     move.w    handle,-(sp)
  1882.     move.w    #$40,-(sp)
  1883.     trap    #1        ;write next buffer full of file
  1884.     adda.w    #12,sp
  1885.     cmp.l    -8(sp),d0
  1886. *
  1887. exactbuf:
  1888.     move.w    handle,-(sp)
  1889.     move.w    #$3e,-(sp)
  1890.     trap    #1            ;close file
  1891.     addq.l    #4,sp
  1892. *
  1893.     bsr    tilewrit
  1894. *
  1895.     moveq    #0,d0            ;return "good"
  1896.     rts
  1897. *
  1898. *
  1899. *    Let's be daring and try the recursive routine technique..
  1900. *
  1901. *
  1902. *  Entry:
  1903. *    d1.w    = hpos adjust
  1904. *    d2.w    = vpos adjust
  1905. *    d3.w    = H & V flip effects XOR bits
  1906. *
  1907. *    a0->    brush # (.w)    =d4
  1908. *        effect    (.w)    =d5
  1909. *        hpos    (.w)    =d6
  1910. *        vpos    (.w)    =d7
  1911. *
  1912. *  local storage requirments:
  1913. *
  1914. LocalSize    equ    16
  1915.     .offset        -LocalSize
  1916. ElementPtr:
  1917.     .ds.l    1        ;ptr to next element of derived
  1918. ElementCnt:
  1919.     .ds.w    1        ;remaining derived elements
  1920. ClustH:
  1921.     .ds.w    1        ;cluster-wide horz offset
  1922. ClustV:
  1923.     .ds.w    1        ;cluster-wide vert offset
  1924. ClustSp:
  1925.     .ds.w    1        ;cluster-wide H & V flip flags
  1926. ClustWid:
  1927.     .ds.w    1        ;cluster width
  1928. ClustHite:
  1929.     .ds.w    1        ;cluster height
  1930.     .text
  1931. *
  1932. brushout:
  1933.     movem.l    a0-a5/d0-d7,-(sp)
  1934.     link    a6,#-LocalSize
  1935. *    movem.w    (a0),d4-d7    ;get brush data in regs
  1936. *
  1937.     lsl.w    #4,d4
  1938.     move.w    (a2,d4.w),ClustWid(a6)
  1939.     move.w    2(a2,d4.w),ClustHite(a6)
  1940.     move.l    8(a2,d4.w),a3    ;get symbol id string ptr
  1941.     move.w    14(a2,d4.w),d0    ;check s_nxln for a rect fill
  1942.     lsr.w    #4,d4
  1943. *
  1944.     cmp.w    maxintrins,d4    ;are we derived or intrinsic?
  1945.     bls    bout0        ;br if intrinsic
  1946. *
  1947. *  A derived brush..
  1948. *
  1949.     tst.w    d0        ;are we a rect fill
  1950.     bne    bout1        ;br if not
  1951. *
  1952. *  This is rect fill  (special)
  1953. *
  1954.     lea    symhead,a1    ;cr,lf,tab,".byte",tab
  1955.     bsr    strngout
  1956. *
  1957.     lsl.w    #4,d4
  1958.     move.w    12(a2,d4.w),d0    ;get color
  1959.     lsr.w    #4,d4
  1960.     bsr    decout        ;put it out
  1961. *
  1962.     lea    boxmsg0,a1    ;tab,tab,"; color",cr,lf etc.
  1963.     bsr    strngout
  1964. *
  1965.     move.w    d6,d0
  1966.     add.w    d1,d0
  1967.     bsr    decout
  1968. *
  1969.     lea    vposhead,a1
  1970.     bsr    strngout
  1971. *
  1972.     move.w    d7,d0
  1973.     add.w    d2,d0
  1974.     bsr    decout
  1975. *
  1976.     lea    vpostail,a1
  1977.     bsr    strngout
  1978. *
  1979.     lea    boxmsg1,a1
  1980.     bsr    strngout
  1981. *
  1982.     move.w    ClustWid(a6),d0
  1983.     bsr    decout
  1984. *
  1985.     lea    boxmsg2,a1
  1986.     bsr    strngout
  1987. *
  1988.     move.w    ClustHite(a6),d0
  1989.     bsr    decout
  1990. *
  1991.     lea    boxmsg3,a1
  1992.     bsr    strngout
  1993. *
  1994.     bra    boutx
  1995. *    
  1996. *
  1997. bout1:
  1998. *
  1999. *  a3 -> derived block entry
  2000. *
  2001.     move.w    4(a3),ElementCnt(a6)    ;save # of elements (-1)
  2002.     lea    6(a3),a3
  2003.     move.l    a3,ElementPtr(a6)    ;point to first element
  2004.     add.w    d6,d1
  2005.     add.w    d7,d2
  2006.     eor.w    d5,d3
  2007.     movem.w    d1-d3,ClustH(a6)    ;save cluster-wide data
  2008. *
  2009. dervloop:
  2010.     move.l    ElementPtr(a6),a0
  2011.     movem.w    (a0),d4-d7    ;get brush data in regs
  2012.     movem.w    ClustH(a6),d1-d3
  2013. *
  2014.     move.w    d3,d0
  2015.     andi.w    #Hflip,d0    ;check for Hflip transform on hoffsets
  2016.     beq    dervlp0
  2017.     move.w    ClustWid(a6),d0
  2018.     sub.w    d6,d0
  2019.     lsl.w    #4,d4
  2020.     sub.w    (a2,d4.w),d0
  2021.     lsr.w    #4,d4
  2022.     move.w    d0,d6        ;hoffset ajusted for hflip
  2023. dervlp0:
  2024.     move.w    d3,d0
  2025.     andi.w    #Vflip,d0    ;check for Vflip transform on hoffsets
  2026.     beq    dervlp1
  2027.     move.w    ClustHite(a6),d0
  2028.     sub.w    d7,d0
  2029.     lsl.w    #4,d4
  2030.     sub.w    2(a2,d4.w),d0
  2031.     lsr.w    #4,d4
  2032.     move.w    d0,d7        ;voffset ajusted for vflip
  2033. dervlp1:
  2034. *
  2035.     bsr    brushout        ;recursively call ourselves
  2036. *
  2037.     adda.w    #12,a0
  2038.     move.l    a0,ElementPtr(a6)
  2039.     subq.w    #1,ElementCnt(a6)
  2040.     bpl    dervloop        ;put out all the elements in derived
  2041.     bra    boutx            ;exit this routine
  2042. *
  2043. *
  2044. *  An intrinsic brush, we write direct
  2045. *
  2046. bout0:
  2047.     lea    symhead,a1
  2048.     bsr    strngout    ;cr,lf,tab,".byte",tab
  2049. *
  2050. *    
  2051. mosymx:
  2052.     move.b    (a3)+,d0
  2053.     cmpi.b    #" ",d0
  2054.     bls    symoutx
  2055.     bsr    charwri
  2056.     bra    mosymx    
  2057. symoutx:
  2058.     lea    symtail,a1
  2059.     bsr    strngout
  2060. *
  2061. *   Put out special effects
  2062. *
  2063.     eor.w    d3,d5
  2064.     move.w    d5,d0
  2065. *    move.w    d3,d0        ;copy passed effects XOR bits
  2066. *    eor.w    d5,d0        ;XOR with local H & V bits
  2067.     andi.w    #3,d0
  2068.     beq    specf        ;skip over H/V if no H & V
  2069. *
  2070.     move.w    d0,-(sp)
  2071.     andi.w    #Hflip,d0
  2072.     beq    noclust1
  2073.     move.b    #"H",d0
  2074.     bsr    charwri
  2075. noclust1:
  2076.     move.w    (sp)+,d0
  2077.     move.w    d0,-(sp)
  2078.     andi.w    #Vflip,d0
  2079.     beq    noclust2
  2080.     move.b    #"V",d0
  2081.     bsr    charwri
  2082. noclust2:
  2083.     move.w    (sp)+,d0
  2084.     lea    keywords+1,a1    ;finish with "flip"
  2085.     bsr    strngout
  2086. *    move.w    d5,d0
  2087. *    or.w    d3,d0
  2088. *
  2089. *    or.w    d3,d5
  2090.     move.w    d5,d0
  2091. *
  2092.     and    #$ffff-(Vflip+Hflip),d0
  2093.     beq    specfo        ;anything in addition to HVflip?
  2094.     move.b    #"+",d0
  2095.     bsr    charwri
  2096.     bra    specf
  2097. specff:
  2098.     bsr    strngout
  2099.     bra    specfo    
  2100. specf:
  2101.     lea    Vstrng,a1
  2102.     move.w    d5,d0
  2103.     andi.w    #Vbridge,d0
  2104.     bne    specff
  2105.     lea    Hstrng,a1
  2106.     move.w    d5,d0
  2107.     andi.w    #Hbridge,d0
  2108.     bne    specff
  2109. *
  2110.     lea    Astrng,a1
  2111.     move.w    d5,d0
  2112.     andi.w    #Alternate,d0
  2113.     bne    specff
  2114. *
  2115.     lea    Fillstrng,a1
  2116.     cmpi.w    #FillRect,d5
  2117.     beq    specff
  2118. *
  2119.     moveq    #0,d0
  2120.     bsr    decout        ;put out signed decimal in d0
  2121. specfo:
  2122.     lea    hposhead,a1
  2123.     bsr    strngout
  2124. *
  2125.     move.w    d6,d0
  2126.     add.w    d1,d0
  2127.     bsr    decout
  2128. *
  2129.     lea    vposhead,a1
  2130.     bsr    strngout
  2131. *
  2132.     move.w    d7,d0
  2133.     add.w    d2,d0
  2134.     bsr    decout
  2135. *
  2136.     lea    vpostail,a1
  2137.     bsr    strngout
  2138. boutx:
  2139.     unlk    a6
  2140.     movem.l    (sp)+,a0-a5/d0-d7
  2141.     rts
  2142. *
  2143. *
  2144. **********************************************
  2145. *
  2146. *    Load a new cluster
  2147. *
  2148. *
  2149. load_cluster:
  2150.     movem.l    a0-a6/d0-d7,-(sp)
  2151. *
  2152.     move.w    #0,-(sp)
  2153.     move.l    a0,-(sp)
  2154.     move.w    #$3d,-(sp)    ;open file for read
  2155.     trap    #1
  2156.     addq.l    #8,sp
  2157. *
  2158.     move.w    d0,handle
  2159.     bmi    rderr        ;exit thru world read if file read error
  2160. *
  2161.     move.l    #undbuf,a4 ;temporarily stuff world data where grafix is
  2162.     bsr    parsetxt    ;get machine readable in buffer at a4
  2163. *
  2164. *    a4 -> end of buffer
  2165. *
  2166.     lea    undbuf,a0
  2167. *
  2168.     bsr    genclust
  2169. *
  2170. *
  2171.     movem.l    (sp)+,a0-a6/d0-d7
  2172. *
  2173.     rts
  2174. *
  2175. *
  2176. *
  2177. *
  2178. *
  2179. *    a0 -> beginning of buffer with cluster items
  2180. *    a4 -> end +1 of this buffer
  2181. *
  2182. genbridg:
  2183.     moveq    #1,d4
  2184.     bra    lust
  2185. genclust:
  2186.     move.w    #$8001,d4
  2187. lust:
  2188.     lea    brshfile,a1
  2189. *
  2190. *  now check each sprite item to see if it needs to be derived
  2191. *
  2192.     move.w    #$7fff,d0    ;max 16-bit signed
  2193.     move.w    d0,d1
  2194.     move.w    #$8000,d2    ;min 16-bit signed
  2195.     move.w    d2,d3
  2196.     movem.w    d0-d3,xmin_clust    ;set-up max min stuff
  2197. spritchk:
  2198.     movem.w    (a0),d1-d2    ;get brush #, effects
  2199.     move.w    d1,curbrush
  2200.  
  2201.     move.w    d2,d3
  2202.     andi.w    #Hflip,d3    ;do we need hflip?
  2203.     beq    lfover4
  2204.     movem.l    a0-a1/a4/d0-d4,-(sp)
  2205.     bsr    makehflp
  2206.     movem.l    (sp)+,a0-a1/a4/d0-d4
  2207. lfover4:
  2208.     andi.w    #Vflip,d2    ;do we need vflip
  2209.     beq    lfover5
  2210.     movem.l    a0-a1/a4/d0-d4,-(sp)
  2211.     bsr    makevflp
  2212.     movem.l    (sp)+,a0-a1/a4/d0-d4
  2213. lfover5:
  2214.     move.l    a0,(a1)+    ;save the cluster item ptr
  2215.     addq.b    #1,d4        ;advance count
  2216. *
  2217.     move.w    curbrush,d0
  2218.     move.w    d0,(a0)+    ;use derived brush
  2219.     clr.w    (a0)+        ;we don't use effects
  2220. *
  2221.     movem.w    (a0)+,d2-d3    ;get hpos/vpos
  2222. *
  2223.     lea    bshblock,a2
  2224.     lsl.w    #4,d0
  2225.     movem.w    (a2,d0.w),d5-d6    ;get width & height
  2226.     add.w    d2,d5
  2227.     add.w    d3,d6
  2228. *
  2229. *  d2 - xmin
  2230. *  d3 - ymin
  2231. *  d5 - xmax +1
  2232. *  d6 - ymax +1
  2233. *
  2234. *  accumulate cluster-wide min/max info
  2235. *
  2236.     cmp.w    xmin_clust,d2
  2237.     bge    aggr50
  2238.     move.w    d2,xmin_clust
  2239. aggr50:
  2240.     cmp.w    ymin_clust,d3
  2241.     bge    aggr60
  2242.     move.w    d3,ymin_clust
  2243. aggr60:
  2244.     cmp.w    xmax_clust,d5
  2245.     ble    aggr70
  2246.     move.w    d5,xmax_clust
  2247. aggr70:
  2248.     cmp.w    ymax_clust,d6
  2249.     ble    aggr80
  2250.     move.w    d6,ymax_clust
  2251. aggr80:
  2252. *
  2253.     cmp.l    a4,a0
  2254.     bcs    spritchk
  2255. *
  2256.     move.w    d4,aggreg
  2257. *
  2258.     bsr    clustit        ;build a new cluster
  2259.     rts
  2260. *
  2261. ***********************************************
  2262. *
  2263. *    Save current cluster
  2264. *
  2265. *        
  2266. *
  2267. *  a0 -> pathname
  2268. *
  2269. save_cluster:
  2270.     move.w    #0,-(sp)
  2271.     move.l    a0,-(sp)
  2272.     move.w    #$3c,-(sp)    ;create file
  2273.     trap    #1
  2274.     addq.l    #8,sp
  2275. *
  2276.     move.w    d0,handle
  2277.     bmi    sbsherr        ;exit if file write error
  2278. *
  2279.     move.l    #undbuf,undform    ;use this for a write buffer
  2280. *
  2281.     lea    bshblock,a2
  2282. *
  2283.     lea    cluhead,a1    ;"--Cluster--"
  2284.     bsr    strngout
  2285. *
  2286.     moveq    #0,d1
  2287.     moveq    #0,d2
  2288.     moveq    #0,d3
  2289.     move.w    curbrush,d4    ;get current brush (it's a cluster)
  2290.     moveq    #0,d5
  2291.     moveq    #0,d6
  2292.     moveq    #0,d7
  2293.     bsr    brushout    ;put it out
  2294. *
  2295. *  all the objects are out--close this guy
  2296. *
  2297.     lea    undbuf,a0
  2298.     move.l    undform,d0
  2299.     sub.l    a0,d0
  2300.     beq    xactbuf
  2301. *
  2302. *  write out partial last buffer
  2303. *
  2304.     move.l    a0,-(sp)
  2305.     move.l    d0,-(sp)
  2306.     move.w    handle,-(sp)
  2307.     move.w    #$40,-(sp)
  2308.     trap    #1        ;write next buffer full of file
  2309.     adda.w    #12,sp
  2310.     cmp.l    -8(sp),d0
  2311. *
  2312. xactbuf:
  2313.     move.w    handle,-(sp)
  2314.     move.w    #$3e,-(sp)
  2315.     trap    #1            ;close file
  2316.     addq.l    #4,sp
  2317. *
  2318.     moveq    #0,d0            ;return "good"
  2319.     rts
  2320. *
  2321. *
  2322. *
  2323. *   a1 -> string to output to write file
  2324. *
  2325. strngout:
  2326.     move.l    d0,-(sp)
  2327. strngo0:
  2328.     move.b    (a1)+,d0
  2329.     beq    outi
  2330.     bsr    charwri
  2331.     bra    strngo0
  2332. outi:
  2333.     move.l    (sp)+,d0
  2334.     rts
  2335. *
  2336. *  Put out signed d0.w as decimal string
  2337. *
  2338. decout:
  2339.     movem.l    a0-a1/d0-d7,-(sp)
  2340.     lea    brshfile,a1    ;use this as a temp buffer
  2341.     move.l    a1,-(sp)
  2342.     lea    decitab(pc),a0
  2343.     moveq    #4,d3        ;up to 5 chars
  2344.     moveq    #0,d2
  2345.     tst.w    d0
  2346.     bpl    decout1
  2347.     neg.w    d0
  2348.     move.b    #"-",(a1)+
  2349. decout1:
  2350.     andi.l    #$ffff,d0
  2351.     divu    (a0)+,d0
  2352.     tst.w    d2        ;have we put out leading non-zero yet?
  2353.     bne    decout2        ;br if so
  2354.     tst.w    d3        ;is this the last chance to put out a char?
  2355.     beq    decout2        ;br if so--put out at least 1 char
  2356.     tst.w    d0        ;else, is it a leading zero?
  2357.     beq    decout3
  2358. decout2:
  2359.     moveq    #-1,d2
  2360.     add.b    #"0",d0
  2361.     move.b    d0,(a1)+
  2362. decout3:
  2363.     swap    d0
  2364.     dbra    d3,decout1
  2365. *
  2366.     clr.b    (a1)+
  2367.     move.l    (sp)+,a1
  2368.     bsr    strngout
  2369.     movem.l    (sp)+,a0-a1/d0-d7
  2370.     rts
  2371. *
  2372. *
  2373. *  d0 = char to write to output file
  2374. *
  2375. charwri:
  2376.     movem.l    a1,-(sp)
  2377.     move.l    undform,a1
  2378.     move.b    d0,(a1)+
  2379.     cmpa.l    #undbuf+4096,a1
  2380.     bcs    chw1
  2381. *
  2382.     lea    undbuf,a1
  2383.  
  2384.     movem.l    a0-a6/d1-d7,-(sp)
  2385.  
  2386.     move.l    a1,-(sp)
  2387.     move.l    #4096,-(sp)
  2388.     move.w    handle,-(sp)
  2389.     move.w    #$40,-(sp)
  2390.     trap    #1        ;write next buffer full of file
  2391.     adda.w    #12,sp
  2392.     cmp.l    -8(sp),d0
  2393.     
  2394.     movem.l    (sp)+,a0-a6/d1-d7
  2395.     beq    chw1
  2396.     move.l    (sp)+,a1
  2397.     moveq    #-1,d0
  2398.     rts
  2399. chw1:
  2400.     move.l    a1,undform
  2401.     move.l    (sp)+,a1
  2402.     moveq    #0,d0
  2403.     rts
  2404. *
  2405. *dum:
  2406. *    bsr    view
  2407. *    rts
  2408. *
  2409.